home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / asm / demos / vectors_hiddenline / hiddenlinevectors.s next >
Text File  |  1980-01-03  |  11KB  |  511 lines

  1.  
  2.     Section Mycode,CODE_C
  3.  
  4. START    JMP RUN(PC)
  5.  
  6.     Include src:textrotatingzoom/hw.s
  7.     include src:textrotatingzoom/trig.s
  8.     even
  9.     
  10. Openlib    equ -552
  11. Closelib equ -414            ;...Equates for OS calls...
  12. Oslist equ 38
  13. Forbid  equ -132
  14. Permit  equ -138
  15.  
  16. gfxname    dc.b    "graphics.library",0,0
  17. storelist dc.l  0
  18.  
  19.  
  20. RUN:
  21. ;-- Close Down the System Set up Bitplanes etc..
  22. ;-- This is the Standard Goldfire Setup Routine (C) Goldfire 1990-1991
  23.     lea custom,a5
  24.     move.l 4.w,a6
  25.     bset #1,ciaa+pra        ;Led Off
  26.     jsr forbid(a6)
  27.     blitwait            ;Multitask down so let any Blits end
  28.     lea gfxname,a1
  29.     moveq #0,d0
  30.     jsr openlib(a6)            ;open gfxlib
  31.     move.l d0,a1            ;Put gfx base addr in a1
  32.     move.l oslist(a1),storelist    ;store OS copper addr
  33.     jsr closelib(a6)        ;close gfxlib        
  34.     catchVB
  35.     move #$0020,dmacon(a5)        ;sprite DMA off
  36.     move #$8640,dmacon(a5)
  37.     move.l    #screen1,d0
  38.     move.w    d0,pl0l
  39.     swap    d0
  40.     move.w    d0,pl0h
  41.     move #$8100,dmacon(a5)
  42.     move #$4020,intena(a5)        ;Master & VB off
  43.     move #$c010,intena(a5)        ;Master & Copper On
  44.     catchVB
  45.     move.l #Copperlist,cop1lc(a5)
  46.     move #$0080,dmacon(a5)
  47.     move #0,copjmp1(a5)
  48.     move #$8080,dmacon(a5)
  49.  
  50. ;-- Main Mouse Waiting Loop
  51.  
  52. mouse:        cmp.b    #255,$dff006
  53.         bne.s    mouse
  54.  
  55.         bsr    Vector_Routine
  56.  
  57.         btst #6,ciaa+pra
  58.         bne.s mouse    
  59.  
  60. ;-- Tidy up afterwards and quit
  61.  
  62.     bclr #1,ciaa+pra        ;Led On
  63.     move #$7e0,dmacon(a5)        ;Dma (incl nasty blit) off
  64.     move.l storelist(pc),cop1lc(a5)
  65.     move #0,copjmp1(a5)
  66.     move #$83e0,dmacon(a5)
  67.     move #$4010,intena(a5)
  68.     move #$c020,intena(a5)
  69.     jsr permit(a6)
  70.     moveq #0,d0
  71.     rts                ;Go Home.....
  72.  
  73. Blanksprite dc.w 0,0
  74.  
  75. ;-- Copper List... PAL and 1 bitplanes
  76. Copperlist:
  77.     dc.w diwstrt,$2a81        
  78.     dc.w diwstop,$2ac1
  79.     dc.w ddfstrt,$38
  80.     dc.w ddfstop,$d0        ;Normal screen
  81.     dc.w bpl1mod,0
  82.     dc.w bpl2mod,0
  83.     dc.w bplcon1,0,$1fc,0,$106,0
  84. cols    dc.w col0,0,col1,$FF,col2,$8,col3,$FF
  85.     dc.w bpl1ptl
  86. pl0l    dc.w 0,bpl1pth
  87. pl0h    dc.w 0,bpl2ptl    
  88. pl1l    dc.w 0,bpl2pth
  89. pl1h    dc.w 0
  90.     dc.w bplcon0,$1200        ;One planes
  91.     dc.w bplcon2,0            ;sprites behind
  92.     dc.w $ffdf,$fffe        ;wait for end of NTSC
  93.     dc.w $ffff,$fffe        ;endless wait
  94.  
  95. *****************************************************************************
  96. ; Okay lets start with nice user-friendly assembler constants for readabilty
  97. *****************************************************************************
  98.  
  99. OriginDist    =    640
  100. ScreenDist    =    640
  101. SNext_Ob    =    0        ;These are offsets in data structure
  102. SXpos        =    4        ;these should always be used
  103. SYpos        =    6        ;incase of change of structure
  104. SZpos        =    8
  105. SXangle        =    10
  106. SYangle        =    12
  107. SZangle        =    14
  108. SNum_Pts    =    16
  109. SCorner_Pt    =    20    
  110. SFace_Pt    =    24    
  111.  
  112. *****************************************************************************
  113. ;-- Main Loop...
  114. *****************************************************************************
  115.  
  116. VECTOR_ROUTINE:
  117.     bsr    DoubleBuffer
  118.     bsr    Blitclear
  119.     bsr    Animate
  120.     bsr    Vector_Calculate
  121.     bsr    DrawObjects
  122.     rts
  123.     
  124. *****************************************************************************
  125. ; 3D Vector Calculatation Routine by Prophet of Goldfire (C) T.Szirtes 1991
  126. ; Features realtime Rotation, Translation, Transformation, HiddenLine Vectors
  127. *****************************************************************************
  128.  
  129. Vector_Calculate:
  130.  
  131. ;-- Load up our pointers and get relevant data
  132.  
  133.     move.l    ObjectPointer,a0    ;Pointer to Object Data in a0
  134.     move.l    SCorner_Pt(a0),a2    ;Pointer to Points in a2
  135.     lea    Screen_Points,a1    ;Pointer to Screen POints,a1
  136.     lea    sinetab,a4        ;SineTable in a4
  137.     move.l  SNum_Pts(a0),d6
  138. VecLoop
  139.  
  140. ;-- Tidy Up
  141.  
  142.     moveq.l    #0,d0
  143.     moveq.l    #0,d1
  144.     moveq.l    #0,d2
  145.  
  146. ;-- Rotation about Z Axis :- X2 = X1 COS a - Y1 SIN a, Y2 = Y1 COS a - X1 SIN a
  147.  
  148.     move SZangle(a0),d5    
  149.     move d5,d4
  150.     sin d4            
  151.     cos d5            
  152.     move (a2),d0        ;X pos
  153.     move 2(a2),d1        ;Y Pos
  154.     muls d5,d0        ;X * cos a
  155.     muls d4,d1        ;Y * sin a
  156.     sub.l d1,d0        ;subtract to get...
  157.     trigdiv d0        ;FINAL X
  158.     
  159.     move (a2),d1        ;X pos
  160.     move 2(a2),d2        ;Y pos
  161.     muls d4,d1        ;X * sin a
  162.     muls d5,d2        ;Y * cos a
  163.     add.l d2,d1        ;add...
  164.     trigdiv d1        ;FINAL Y
  165.  
  166. ;-- Rotation about X Axis :- Y2 = Y1 COS a - Z1 SIN a, Z2 = Z1 COS a + Y1 SIN a
  167.  
  168.     move SXangle(a0),d5    
  169.     move d5,d4
  170.     sin d4            
  171.     cos d5            
  172.  
  173.     move 4(a2),d2        ;Find Z
  174.     move d1,d7        ;X in d0, Y in d7, Z in d2
  175.  
  176.     muls d5,d1        ;Y * cos a
  177.     muls d4,d2        ;Z * sin a
  178.     sub.l d2,d1        ;subtract to get...
  179.     trigdiv d1        ;FINAL Y
  180.     
  181.     move 4(a2),d2        ;Find Z
  182.     muls d4,d7        ;Y * sin a
  183.     muls d5,d2        ;Z * cos a
  184.     add.l d7,d2        ;add...
  185.     trigdiv d2        ;FINAL Z
  186.  
  187. ;-- Rotation about Y Axis :- Z2 = Z1 COS a - X1 SIN a, X2 = X1 COS a + Z1 SIN a
  188.  
  189.     move SYangle(a0),d5    
  190.     move d5,d4
  191.     sin d4            
  192.     cos d5            
  193.  
  194.     move d2,d7
  195.     move d0,d3        ;X in d3, Y in d1, Z in d7
  196.  
  197.     muls d5,d2        ;Z * cos a
  198.     muls d4,d3        ;X * sin a
  199.     sub.l d2,d3        ;subtract to get...
  200.     trigdiv d3        ;FINAL Z
  201.     move d3,d2
  202.  
  203.     muls d4,d7        ;Z * sin a
  204.     muls d5,d0        ;X * cos a
  205.     add.l d7,d0        ;add...
  206.     trigdiv d0        ;FINAL Z
  207.  
  208. ;-- Okay lets calculate perspective
  209.  
  210.     add SZpos(a0),d2    ;Add its Zposition
  211.  
  212.     add #origindist,d2    ;Calculate Perspective
  213.     muls #screendist,d0
  214.     muls #screendist,d1
  215.     divs d2,d0
  216.     divs d2,d1
  217.  
  218. ;-- Then lets move it to the right position (Translate)
  219.  
  220.     add.w    SXpos(a0),d0    ;Add Xpos
  221.     add.w    SYpos(a0),d1    ;Add Ypos
  222.  
  223. ;- and put it into the list
  224.  
  225.     move.w    d0,(a1)+
  226.     move.w    d1,(a1)+
  227.  
  228. ;- loop
  229.  
  230.     add.l    #6,a2        ;Next load of points
  231.     dbra    d6,VecLoop
  232.  
  233. Finished_Points
  234.     rts
  235.  
  236. *****************************************************************************
  237. ; Draw Objects -- This Incorporates the Hidden Line Routine 
  238. *****************************************************************************
  239.  
  240. DrawObjects:
  241.     move.l    objectpointer,a0
  242.     move.l    SFace_pt(a0),a1        ;Pointer to Face Structure
  243.     lea    Screen_Points,a2    ;Pointer to Points
  244.  
  245. HiddenLineLoop
  246.     move    (a1)+,d7        ;d7 = number of points
  247.     bmi    NomoreFaces        ;if -1 then no more faces
  248.  
  249.     move.w    (a1),d0            ;d0 = Offset to Point 1
  250.     move.w    (a2,d0),d1        ;d1 = X1
  251.     move.w    2(a2,d0),d2        ;d2 = Y1
  252.     move.w    2(a1),d0        ;d0 = Offset to Point 2
  253.     move.w    (a2,d0),d3        ;d3 = X2
  254.     move.w    2(a2,d0),d4        ;d4 = Y2
  255.     sub.w    d1,d3            ;d3 = X21 = X2-X1
  256.     sub.w    d2,d4            ;d4 = Y21 = Y2-Y1
  257.  
  258.     move.w    4(a1),d0        ;d0 = Offset to Point 3
  259.     move.w    (a2,d0),d5        ;d5 = X3
  260.     move.w    2(a2,d0),d6        ;d6 = Y3
  261.     sub.w    d1,d5            ;d5 = X31 = X3-X1
  262.     sub.w    d2,d6            ;d6 = Y31 = Y3-Y1
  263.                     ;Phew just enough registers!
  264.                     ;try doing that on an 8bit!
  265.     muls    d3,d6            ;X21*Y31
  266.     muls    d5,d4            ;X31*Y21
  267.     sub.l    d4,d6            ;subtracted
  268.     bmi    Face_seen        ;If its positive we can see it
  269.     add.w    #2,d7
  270.     add.l    d7,d7
  271.     lea    (a1,d7.w),a1        ;Find next face
  272.     bra.s    HiddenLineLoop        ;otherwise Loop
  273.     rts
  274. Face_Seen
  275. ;-- We now have to load up the registers ready for line drawing
  276. ;-- a1 face structure, a2 - points
  277. Face_Seen_Loop
  278.     moveq.l    #0,d0
  279.     moveq.l    #0,d1
  280.     moveq.l    #0,d2
  281.     moveq.l    #0,d3
  282.     move.w    (a1)+,d4        ;pointer to Point 1
  283.     move.w    (a1),d5            ;pointer to Point 2
  284.  
  285.     move.w    (a2,d4),d0        ;
  286.     move.w    2(a2,d4),d1
  287.  
  288.     move.w    (a2,d5),d2
  289.     move.w    2(a2,d5),d3
  290.     bsr    linedraw
  291.     dbra    d7,Face_Seen_Loop
  292.     add.w    #2,a1
  293.     jmp    HiddenLineLoop
  294. Nomorefaces
  295. ;    move.w    #$FFF,$DFF180
  296.     rts
  297.  
  298. *****************************************************************************
  299. ;-- Animate Routine... Basically add velocities to variables
  300. *****************************************************************************
  301.  
  302. Animate:
  303.     move.l    ObjectPointer,a0
  304.     move.w ANGLEVX,d0
  305.     add.w d0,SXangle(a0)
  306.     move.w ANGLEVY,d0
  307.     add.w d0,SYangle(a0)
  308.     move.w ANGLEVZ,d0
  309.     add.w d0,SZangle(a0)
  310.     move.w ZVel,d0
  311.     add.w d0,SZpos(a0)
  312.     rts
  313.  
  314.  
  315. ;-- Data for 3D routine... 
  316.  
  317. ANGLEVX    dc.w    1
  318. ANGLEVZ    dc.w    4
  319. ANGLEVY dc.w     2
  320. ZVel    dc.w    0
  321. ObjectPointer dc.l    Object1
  322. screen_Points    dcb.w    90*2
  323. NulObject    dc.l    Object1,0,0
  324.     dc.w    0,0,0,0,0,0,0,0,0,0
  325.  
  326. *****************************************************************************
  327. ;-- 3D Data Structure
  328. ;-- Format    dc.l Pointer to nextobject (0 if no more)    0
  329. ;        dc.w XPos,YPos,Zpos,Xangle,YAngle,ZAngle    4,6,8,10,12,14
  330. ;        dc.l Pointer to Corners                16
  331. ;        dc.l Pointer to Faces                20
  332. *****************************************************************************
  333. ;Boring Cube
  334. OBJECT1 dc.l 0                ;Next Object
  335.     dc.w 160,128,0,0,0,0        ;Xpos,Ypos,Zpos,Xa,ya,za
  336.     dc.l 7                ;number of pts-1
  337.     dc.l PTS2,FAC2            ;Pointer to points/faces
  338. pts2:    dc.w -50,-50,-50
  339.     dc.w 50,-50,-50
  340.     dc.w 50,50,-50
  341.     dc.w -50,50,-50
  342.     dc.w -50,-50,50
  343.     dc.w 50,-50,50
  344.     dc.w 50,50,50
  345.     dc.w -50,50,50 
  346. fac2:     dc.w 3,0*4,1*4,2*4,3*4,0*4    ;Numberofpoints then points
  347.     dc.w 3,1*4,5*4,6*4,2*4,1*4    ;dont forget to repeat first
  348.     dc.w 3,4*4,7*4,6*4,5*4,4*4    ;Points are entered clockwise
  349.     dc.w 3,0*4,3*4,7*4,4*4,0*4    ;and bloody confusing it is
  350.     dc.w 3,2*4,6*4,7*4,3*4,2*4
  351.     dc.w 3,0*4,4*4,5*4,1*4,0*4
  352.     dc.w -1
  353. ;Cool Ship
  354. OBJECT2 dc.l 0                ;Next Object
  355.     dc.w 160,128,0,0,0,0        ;Xpos,Ypos,Zpos,Xa,ya,za
  356.     dc.l 4                 ;number of pts-1
  357.     dc.l PTS1,FAC1            ;Pointer to points/faces
  358. pts1:    
  359.     dc.w 0,0,-50
  360.     dc.w 70,0,50
  361.     dc.w 0,30,50
  362.     dc.w -70,0,50
  363.     dc.w 0,-30,50
  364.     
  365. fac1:     dc.w 2,0*4,1*4,2*4,0*4
  366.     dc.w 2,0*4,2*4,3*4,0*4
  367.     dc.w 2,0*4,4*4,1*4,0*4
  368.     dc.w 2,0*4,3*4,4*4,0*4
  369.     dc.w 3,1*4,4*4,3*4,2*4,1*4
  370.     dc.w -1
  371.  
  372. *****************************************************************************
  373. ;-- Toggle Screen Routine
  374. *****************************************************************************
  375.  
  376. DoubleBuffer:
  377.     move.l    Scrpt1,a0
  378.     move.l    Scrpt2,a1
  379.     move.l    a0,Scrpt2
  380.     move.l    a1,Scrpt1
  381.     move.l    a1,d0
  382.     move.w    d0,pl0l
  383.     swap    d0
  384.     move.w    d0,pl0h
  385.     rts
  386. Scrpt1    dc.l    Screen1
  387. Scrpt2    dc.l    Screen2
  388.  
  389. *****************************************************************************
  390. ;-- Blit Simple Lines with boundary checking
  391. ;-- Input d0,d1,d2,d3 for X1,Y1,X2,Y2    Uses d4+d5 for working
  392. *****************************************************************************
  393.     
  394. linedraw:
  395.     cmp.l    #320,d0
  396.     bgt    BoundFound
  397.     cmp.l    #0,d0
  398.     blt    BoundFound
  399.  
  400.     cmp.l    #320,d2
  401.     bgt    BoundFound
  402.     cmp.l    #0,d2
  403.     blt    BoundFound
  404.  
  405.     cmp.l    #256,d1
  406.     bgt    BoundFound
  407.     cmp.l    #0,d1
  408.     blt    BoundFound
  409.  
  410.     cmp.l    #256,d3
  411.     bgt    BoundFound
  412.     cmp.l    #0,d3
  413.     blt    BoundFound
  414.  
  415.     move.l    #40,d4
  416.     move.l    scrpt2,a0
  417.     sub    d0,d2
  418.     bmi    xneg
  419.     sub    d1,d3
  420.     bmi    yneg
  421.     cmp    d3,d2
  422.     bmi    ygtx
  423.     moveq.l    #(4*4)!1,d5
  424.     bra    lineagain
  425. ygtx:    exg    d2,d3
  426.     moveq.l    #(0*4)!1,d5
  427.     bra    lineagain
  428. yneg    neg    d3
  429.     cmp.w    d3,d2
  430.     bmi    ynygtx
  431.     moveq.l    #(6*4)!1,d5
  432.     bra    lineagain
  433. ynygtx    exg    d2,d3
  434.     moveq.l    #(1*4)!1,d5
  435.     bra    lineagain
  436. xneg    neg    d2
  437.     sub    d1,d3
  438.     bmi    xyneg
  439.     cmp    d3,d2
  440.     bmi    xnygtx
  441.     moveq.l    #(5*4)!1,d5
  442.     bra    lineagain
  443. xnygtx    exg    d2,d3
  444.     moveq.l    #(2*4)!1,d5
  445.     bra    lineagain
  446. xyneg    neg    d3
  447.     cmp    d3,d2
  448.     bmi    xynygtx
  449.     moveq.l    #(7*4)!1,d5
  450.     bra    lineagain
  451. xynygtx    exg    d2,d3
  452.     moveq.l    #(3*4)!1,d5
  453. lineagain:
  454.     mulu    d4,d1
  455.     ror.l    #4,d0    
  456.     add    d0,d0
  457.     add.l    d1,a0
  458.     add    d0,a0
  459.     swap    d0
  460.     or.w    #$BFA,d0
  461.     lsl.w    #2,d3
  462.     add    d2,d2
  463.     move    d2,d1
  464.     lsl    #5,d1
  465.     add    #$42,d1
  466.     blitwait
  467.     move    d3,Bltbmod(A5)
  468.     sub    d2,d3
  469.     ext.l    d3
  470.     move.l    d3,Bltapth(a5)
  471.     bpl    lineover
  472.     or    #$40,d5
  473. lineover:
  474.     move.w    d0,Bltcon0(a5)
  475.     move.w    d5,Bltcon1(a5)
  476.     move.w    d4,Bltcmod(a5)
  477.     move    d4,Bltdmod(a5)
  478.     sub    d2,d3
  479.     move    d3,BltAmod(A5)
  480.     move    #$8000,BltAdat(A5)
  481.     moveq.l    #-1,d5
  482.     move.l    d5,BltAfwm(a5)
  483.     move.l    a0,BltCpth(a5)
  484.     move.l    a0,BltDpth(a5)
  485.     move    d1,Bltsize(a5)
  486.     rts
  487. BoundFound    
  488.     move.w    #$F,$DFF180    
  489.     rts
  490.  
  491. *****************************************************************************
  492. ; Blitter Clear Routine... one bitplane
  493. *****************************************************************************
  494.  
  495. Blitclear:
  496.     move.w    #$0100,bltcon0(a5)
  497.     move.w    #0,bltcon1(a5)
  498.     move.w    #0,bltdmod(a5)
  499.     move.l    scrpt2,bltdpth(a5)
  500.     move.w    #(256<<6)!20,bltsize(a5)
  501.     blitwait
  502.     rts
  503.  
  504. *****************************************************************************
  505. ; Screens...
  506. *****************************************************************************
  507.  
  508. screen1: ds.b    40*260*1
  509. screen2: ds.b    40*260*1
  510.  
  511.